﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Diplomova_prace
{
    class Polynoms
    {
        public List<double> Soucet(List<double> P1, List<double> P2)
        {
            List<double> pol1 = new List<double>(P1);
            List<double> pol2 = new List<double>(P2);
            int MaxLength = Math.Max(P1.Count,P2.Count);
            if (pol1.Count != P2.Count)
            {
                int addition = P2.Count - pol1.Count;
                if (addition > 0)
                {
                    for (int i = 0; i < addition; i++)
                        pol1.Add(0);
                }
                else
                {
                    addition = Math.Abs(addition);
                    for (int i = 0; i < addition; i++)
                        pol2.Add(0);
                }                  
            }
            List<double> P1sumP2 = new List<double>();
            for (int i = 0; i < MaxLength; i++)
            {
                P1sumP2.Add(pol1[i] + pol2[i]); 
            }
           
            return P1sumP2;
        }
        public List<double> Rozdil(List<double> P1, List<double> P2)
        {
            List<double> pol1 = new List<double>(P1);
            List<double> pol2 = new List<double>(P2);
            int MaxLength = Math.Max(P1.Count, P2.Count);
            if (pol1.Count != P2.Count)
            {
                int addition = P2.Count - pol1.Count;
                if (addition > 0)
                {
                    for (int i = 0; i < addition; i++)
                        pol1.Add(0);
                }
                else
                {
                    addition = Math.Abs(addition);
                    for (int i = 0; i < addition; i++)
                        pol2.Add(0);
                }
            }
            List<double> P1subP2 = new List<double>();
            for (int i = 0; i < MaxLength; i++)
            {
                P1subP2.Add(pol1[i] - pol2[i]);
            }

            return P1subP2;
        }
        public List<double> Nasobeni(List<double> P1, List<double> P2)
        {
            List<double> pol1 = new List<double>(P1);
            List<double> pol2 = new List<double>(P2);
            int pol1Length = pol1.Count;
            int pol2Length = pol2.Count;
            int degreeResultPolynom = pol1.Count + pol2.Count - 1;
            List<double> P1mulP2 = new List<double>();
            for (int i = 0; i < degreeResultPolynom - pol1Length; i++)
            {
                pol1.Add(0);
            }
            for (int i = 0; i < degreeResultPolynom - pol2Length; i++)
            {
                pol2.Add(0);
            }
            for (int i = 0; i < degreeResultPolynom; i++)
            {
                double coeficient = 0;
                for (int j = 0; j < i + 1; j++)
                {
                    coeficient += (pol1[j] * pol2[i - j]);
                }
                P1mulP2.Add(coeficient);
            }
            return P1mulP2;
        }
        public double Horner(List<double> polynom, double X)
        {
            int minX = (int)X;
            int maxX = (int)X + 1;
          List<double> pol = new List<double>(polynom);
          pol.Reverse();
          double fx = 0;
            for (int i = minX; i < maxX+1; i++)
            {
                 fx = (double)pol[0];
                for (int j = 1; j < pol.Count; j++)
                {
                    fx *= X;
                    fx += (double) pol[j];
                     
                }
              //  hornerPol.Add(x);
            }
           // return hornerPol;
            return fx;
        }
        public List<double> Derivace(List<double> polynom)
        {
            List<double> derivace = new List<double>();
            for (int i = 0; i < polynom.Count; i++)
            {
                derivace.Add(polynom[i] * i);
            }
            derivace.RemoveAt(0);
            return derivace;
        }

    }
}
